const formatTime = (date) => {
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(
		':');
};

const formatNumber = (n) => {
	n = n.toString();
	return n[1] ? n : '0' + n;
};

const debounce = (fn, delay) => {
	let t = delay || 500;
	let timer;
	return function() {
		clearTimeout(timer);
		timer = setTimeout(() => {
			// 改变this指向并传递参数
			fn.apply(this, arguments);
		}, t);
	};
};

const throttle = (fun, time) => {
	let t1 = 0; //初始时间

	return function() {
		let t2 = new Date(); //当前时间

		if (t2 - t1 > time) {
			fun.apply(this, arguments);
			t1 = t2;
		}
	};
};

const handleImg = (text) => {
	return text.replace(/\<img/g, '<img style="width: 100%;height:auto;display:block;"');
};

module.exports = {
	formatTime: formatTime,
	debounce: debounce,
	throttle: throttle,
	formatNumber: formatNumber,
	handleImg: handleImg
};